package com.pmoc.framework.client.web;

import com.pmoc.framework.client.service.DataService;
import com.pmoc.framework.core.client.CallBack;
import com.pmoc.framework.core.model.Entity;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * DataController
 *
 * @author humdeef
 * @summary DataController
 * @since 2019-05-16 19:50
 */
@RestController
public class DataController {

    @Autowired
    private Environment environment;

    @Autowired
    private DataService dataService;

    @RequestMapping("add/{count}")
    public void add(@PathVariable Long count) {
        if (count == null) {
            count = 1L;
        }
        for (long i = 0; i < count; i++) {
            Entity entity = new Entity();
            String appName = environment.getProperty("spring.application.name");
            entity.setAppName(appName);
            entity.setTid(i);
            if (i % 3 == 0) {
                entity.setTid(i * 5);
            }
            entity.setExistGap(false);
            entity.setValue("2");
            Boolean result = dataService.add(entity);
        }
    }

    @RequestMapping("random/{count}")
    public void random(@PathVariable Long count) {
        if (count == null) {
            count = 1L;
        }
        for (long i = 0; i < count; i++) {
            Entity entity = new Entity();
            String appName = environment.getProperty("spring.application.name");
            entity.setAppName(appName);
            entity.setTid(RandomUtils.nextLong(0, 10));
            entity.setExistGap(false);
            entity.setValue("2");
            Boolean result = dataService.add(entity);
        }
    }

    @RequestMapping("test1")
    public void test1() {

        Entity entity = new Entity();
        String appName = environment.getProperty("spring.application.name");
        entity.setAppName(appName);
        entity.setTid(6L);
        entity.setExistGap(false);
        entity.setValue("2");
        dataService.add(entity);

        entity = new Entity();
        entity.setAppName(appName);
        entity.setTid(8L);
        entity.setExistGap(false);
        entity.setValue("2");
        dataService.add(entity);

    }

    @RequestMapping("test/{count}")
    public void test(@PathVariable Long count) {
        if (count == null) {
            count = 1L;
        }
        for (long i = 0; i < count; i++) {
            Entity entity = new Entity();
            String appName = environment.getProperty("spring.application.name");
            entity.setAppName(appName);
            entity.setTid(i);
            entity.setExistGap(false);
            entity.setValue("2");
            Boolean result = dataService.add(entity);
        }
    }

    @RequestMapping("test2")
    public void test2() {

        Entity entity = new Entity();
        String appName = environment.getProperty("spring.application.name");
        entity.setAppName(appName);
        entity.setTid(7L);
        entity.setExistGap(false);
        entity.setValue("2");
        Boolean result = dataService.add(entity, new CallBack() {
            @Override
            public Boolean onSuccess(Entity entity, boolean result, String msg) {
                System.out.println(msg);
                return true;
            }

            @Override
            public Boolean onFail(Entity entity, boolean result, String msg, Exception e) {
                System.out.println(msg);
                return true;
            }
        });
    }
}
